home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte16 / crtpal.c < prev    next >
C/C++ Source or Header  |  1996-01-31  |  10KB  |  279 lines

  1.  
  2. #include <windows.h>  
  3. #include "CrtPal.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. #define NUMBERCOLORS 16
  108.  
  109. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  110. {
  111. static LPLOGPALETTE pLogPal;
  112. static HPALETTE     hPal;
  113. static int          nPalSize;
  114.  
  115.    switch( uMsg )
  116.    {
  117.        case WM_CREATE : 
  118.                {
  119.                   int i;
  120.  
  121.                   nPalSize = NUMBERCOLORS;
  122.  
  123.                   // Allocate space for palette entries.
  124.                   //....................................
  125.                   pLogPal = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
  126.                                       sizeof( LOGPALETTE ) +
  127.                                       (NUMBERCOLORS*sizeof( PALETTEENTRY )) );
  128.  
  129.                   // Initialize palette entries with 15 shades of gray.
  130.                   //...................................................
  131.                   pLogPal->palVersion    = 0x300;
  132.                   pLogPal->palNumEntries = NUMBERCOLORS;
  133.  
  134.                   for ( i = 0; i < NUMBERCOLORS-1; i++ )
  135.                   {
  136.                      pLogPal->palPalEntry[i].peRed   = 255 - (i*15);
  137.                      pLogPal->palPalEntry[i].peGreen = 255 - (i*15);
  138.                      pLogPal->palPalEntry[i].peBlue  = 255 - (i*15);
  139.                      pLogPal->palPalEntry[i].peFlags = PC_RESERVED;
  140.                   }
  141.  
  142.                   // Set black palette entry.
  143.                   //.........................
  144.                   pLogPal->palPalEntry[i].peRed   = 0;
  145.                   pLogPal->palPalEntry[i].peGreen = 0;
  146.                   pLogPal->palPalEntry[i].peBlue  = 0;
  147.                   pLogPal->palPalEntry[i].peFlags = PC_RESERVED;
  148.  
  149.                   // Create palette.
  150.                   //................
  151.                   hPal = CreatePalette( pLogPal );
  152.                }
  153.                break;
  154.  
  155.        case WM_PAINT :
  156.                {
  157.                   PAINTSTRUCT ps;
  158.                   HBRUSH      hBrush;
  159.                   int         i;
  160.  
  161.                   BeginPaint( hWnd, &ps );
  162.  
  163.                   // Select and realize palette.
  164.                   //............................
  165.                   SelectPalette( ps.hdc, hPal, FALSE );
  166.                   RealizePalette( ps.hdc );
  167.  
  168.                   SelectObject( ps.hdc, GetStockObject( NULL_PEN ) );
  169.  
  170.                   // Paint a sample of each color in the palette.
  171.                   //.............................................
  172.                   for ( i = 0; i < nPalSize; i++ )
  173.                   {
  174.                       hBrush = CreateSolidBrush( PALETTEINDEX( i ) );
  175.                       SelectObject( ps.hdc, hBrush );
  176.                       Rectangle( ps.hdc, i*10, 0, (i*10)+11, 50 );
  177.                       SelectObject( ps.hdc, GetStockObject( WHITE_BRUSH ) );
  178.                       DeleteObject( hBrush );
  179.                   }
  180.  
  181.                   EndPaint( hWnd, &ps );
  182.                }
  183.                break;
  184.  
  185.       case WM_COMMAND :
  186.               switch( LOWORD( wParam ) )
  187.               {
  188.                  case IDM_TEST :
  189.                          {
  190.                             // Add more entries to palette for 
  191.                             // 16 shades of blue.
  192.                             //................................
  193.                             if ( nPalSize == NUMBERCOLORS &&
  194.                                  ResizePalette( hPal, NUMBERCOLORS*2 ) )
  195.                             {
  196.                                int          i;
  197.                                PALETTEENTRY pEntry;
  198.  
  199.                                nPalSize += NUMBERCOLORS;
  200.  
  201.                                // Initialize each of the new palette entries
  202.                                // with gradient shades of blue.
  203.                                //...........................................
  204.                                for( i = 0; i < NUMBERCOLORS/2; i++ )
  205.                                {
  206.                                   pEntry.peRed   = 0;
  207.                                   pEntry.peGreen = 0;
  208.                                   pEntry.peBlue  = i*30;
  209.                                   pEntry.peFlags = PC_RESERVED;
  210.                                   
  211.                                   SetPaletteEntries( hPal, i+NUMBERCOLORS, 
  212.                                                      1, &pEntry );
  213.                                }
  214.  
  215.                                for( ; i < NUMBERCOLORS; i++ )
  216.                                {
  217.                                   pEntry.peRed   = (i-(NUMBERCOLORS/2))*30;
  218.                                   pEntry.peGreen = (i-(NUMBERCOLORS/2))*30;
  219.                                   pEntry.peBlue  = 255;
  220.                                   pEntry.peFlags = PC_RESERVED;
  221.                                   
  222.                                   SetPaletteEntries( hPal, i+NUMBERCOLORS, 
  223.                                                      1, &pEntry );
  224.                                }
  225.  
  226.                                // Force repainting of the window.
  227.                                //................................
  228.                                InvalidateRect( hWnd, NULL, TRUE );
  229.                             }
  230.                          }
  231.                          break;
  232.  
  233.                  case IDM_ABOUT :
  234.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  235.                         break;
  236.  
  237.                  case IDM_EXIT :
  238.                         DestroyWindow( hWnd );
  239.                         break;
  240.               }
  241.               break;
  242.       
  243.       case WM_DESTROY :
  244.                HeapFree( GetProcessHeap(), 0, pLogPal );
  245.                DeleteObject( hPal );
  246.                PostQuitMessage(0);
  247.                break;
  248.  
  249.       default :
  250.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  251.    }
  252.  
  253.    return( 0L );
  254. }
  255.  
  256.  
  257. LRESULT CALLBACK About( HWND hDlg,           
  258.                         UINT message,        
  259.                         WPARAM wParam,       
  260.                         LPARAM lParam)
  261. {
  262.    switch (message) 
  263.    {
  264.        case WM_INITDIALOG: 
  265.                return (TRUE);
  266.  
  267.        case WM_COMMAND:                              
  268.                if (   LOWORD(wParam) == IDOK         
  269.                    || LOWORD(wParam) == IDCANCEL)    
  270.                {
  271.                        EndDialog(hDlg, TRUE);        
  272.                        return (TRUE);
  273.                }
  274.                break;
  275.    }
  276.  
  277.    return (FALSE); 
  278. }
  279.